using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Web;
using MySql.Data.MySqlClient;
namespace LdExpress
{
    /// <summary>
    /// 
    /// </summary>
    public class LocalCharges
    {
        public long ID = 0;
        public long CarrID = 0;
        public long PortID = 0;
        public string CountryCode = "";
        public string Note = "";
        public LocalCharges()
        {
        }
        public LocalCharges(long lID)
        {
            this.ID = lID;
        }
        public void Save(MySqlConnection cn, MySqlTransaction trans)
        {
            string sqlCommand = "";
            if (ID <= 0) // new object, we insert new record to database
            {
                // build SQL statement
                sqlCommand = "Insert into tbl_LocalCharges (" +
                             "CarrID," +
                             "PortID," +
                             "CountryCode," +
                             "Note) " +
                         " values(" +
                             CarrID.ToString() + "," +
                             PortID.ToString() + "," +
                             "'" + DBModule.RefineString(CountryCode) + "'," +
                             "'" + DBModule.RefineString(Note) + "')";
                // run SQL statement
                DBModule.ExecuteNonQuery(sqlCommand, cn, trans);
                sqlCommand = "Select Max(ID) from tbl_LocalCharges";
                this.ID = long.Parse(DBModule.ExecuteQueryForOneResult(sqlCommand, cn, trans));

            }
            else // edit object, we update old record in database
            {
                // build SQL statement
                sqlCommand = "Update tbl_LocalCharges set " +
                             "CarrID=" + CarrID.ToString() + "," +
                             "PortID=" + PortID.ToString() + "," +
                             "CountryCode='" + DBModule.RefineString(CountryCode) + "'," +
                             "Note='" + DBModule.RefineString(Note) + "'" +
                    " where ID=" + ID.ToString();
                // run SQL statement
                DBModule.ExecuteNonQuery(sqlCommand, cn, trans);
            }
        }
        public void Delete(MySqlConnection cn, MySqlTransaction trans)
        {
            //Buil SQL Statement
            //Run SQL Statement
            string sqlCommand = "DELETE FROM tbl_LocalCharges WHERE ID = " + this.ID.ToString() + " limit 1";
            DBModule.ExecuteNonQuery(sqlCommand, cn, trans);
        }
        public void Load(MySqlConnection cn, MySqlTransaction trans)
        {
            string sqlCommand = "";
            //Build SQL Statement
            //Run SQL Statement
            sqlCommand = "SELECT * FROM tbl_LocalCharges WHERE ID = " + ID.ToString() + " limit 0,1";
            DataSet ds = DBModule.ExecuteQuery(sqlCommand, cn, trans);
            //Fill data into this object
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (!dr.IsNull("Id"))
                    ID = long.Parse(dr["Id"].ToString());
                if (!dr.IsNull("CarrID"))
                    CarrID = long.Parse(dr["CarrID"].ToString());
                if (!dr.IsNull("PortID"))
                    PortID = long.Parse(dr["PortID"].ToString());
                if (!dr.IsNull("CountryCode"))
                    CountryCode = dr["CountryCode"].ToString();
                if (!dr.IsNull("Note"))
                    Note = dr["Note"].ToString();
            }
            else
            {
            }
        }
        public void Load(long carrID, long portID, string countryCode, MySqlConnection cn, MySqlTransaction trans)
        {
            string sqlCommand = "";
            //Build SQL Statement
            //Run SQL Statement
            sqlCommand = "SELECT * FROM tbl_LocalCharges WHERE CarrID = " + carrID.ToString() + " AND PortID=" + portID +
                " AND CountryCode='" + DBModule.RefineString(countryCode) + "'  limit 0,1";
            DataSet ds = DBModule.ExecuteQuery(sqlCommand, cn, trans);
            //Fill data into this object
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (!dr.IsNull("Id"))
                    ID = long.Parse(dr["Id"].ToString());
                if (!dr.IsNull("CarrID"))
                    CarrID = long.Parse(dr["CarrID"].ToString());
                if (!dr.IsNull("PortID"))
                    PortID = long.Parse(dr["PortID"].ToString());
                if (!dr.IsNull("CountryCode"))
                    CountryCode = dr["CountryCode"].ToString();
                if (!dr.IsNull("Note"))
                    Note = dr["Note"].ToString();
            }
            else
            {
            }
        }
        public static void GetList(string Condition, string OrderBy, int NumOfItemsPerPage, int PageNo, out int NumberOfPages, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            NumberOfPages = 0;
            string sqlCommand = "";
            ds = null;
            // build SQL statement
            sqlCommand = "Select * from tbl_LocalCharges WHERE 1=1 ";
            if (Condition.Length > 0)
            {
                sqlCommand += Condition;
            }

            if ((OrderBy != null) && (OrderBy != ""))
                sqlCommand = sqlCommand + " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            if (NumOfItemsPerPage > 0)
            {
                ds = DBModule.ExecuteQuery(sqlCommand, NumOfItemsPerPage, PageNo, out NumberOfPages, cn, trans);
            }
            else
                ds = DBModule.ExecuteQuery(sqlCommand, cn, trans);
        }
        public static void DoSearch(string Condition, string OrderBy, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            string sqlCommand = "";
            ds = null;
            // build SQL statement
            sqlCommand = "Select * from tbl_LocalCharges where 1=1 ";
            if (Condition.Length > 0)
                sqlCommand += Condition;
            if ((OrderBy != null) && (OrderBy != ""))
                sqlCommand = sqlCommand + " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            ds = DBModule.ExecuteQuery(sqlCommand, cn, trans);
        }
    }
}
